//
//  SRSilenceClockView.m
//  SportRing
//
//  Created by icelee on 14-11-1.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRSilenceClockView.h"
#import "UIScrollView+SVPullToRefresh.h"
#import "SRDB.h"

@interface SRSilenceClockView()<SRSilenceClockTableViewCellDelegate>

@property (nonatomic, weak)IBOutlet UITableView *srTableView;
@property (nonatomic, strong)NSArray *clocks;

@end

@implementation SRSilenceClockView

- (void)awakeFromNib
{
    [self.srTableView addPullToRefreshWithActionHandler:^{
        
        if (_srSilenceDelegate && [_srSilenceDelegate respondsToSelector:@selector(updateAlarmData)])
        {
            [_srSilenceDelegate updateAlarmData];
        }
    }];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTableview) name:@"updateTableview" object:nil];
    self.srTableView.bounces = NO;
}

- (void)updateTableview
{
    [self performSelector:@selector(ssss) withObject:nil afterDelay:0];
}

- (void)ssss
{
     [_srSilenceDelegate updateAlarmData];
}

- (void)refreshData
{
    [_srTableView triggerPullToRefresh];
}

- (void)stopAnimating
{
    [_srTableView.pullToRefreshView stopAnimating];
}


- (void)setClocks:(NSArray *)clocks
{
    if (_clocks == clocks)
    {
        return;
    }
    
    _clocks = clocks;
    
    [self.srTableView reloadData];
}


#pragma mark - UITableViewDataSource ,UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    return [_clocks count] >0 ?[_clocks count] : 0;
    
}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

{
    UIView *footerView = nil;
    
    footerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.srTableView.frame.size.width, 200)];
    
    footerView.backgroundColor=[UIColor clearColor];
    
    return footerView;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 200;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    SRSilenceClockTableViewCell *silenceCell = [tableView dequeueReusableCellWithIdentifier:@"SRSilenceClockCell"];
    
    SRSilenceClockTableViewCellModel *silenceCellModel = (SRSilenceClockTableViewCellModel *)[_clocks objectAtIndex:indexPath.row];
    
    silenceCell.model = silenceCellModel;
    
    [silenceCell setSRSilenceClockTableViewCellData:silenceCellModel];
  
    return silenceCell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SRMe *me = [SRMe currentUser];
    SRDevice * device = me.device;
    if (!device) {
        UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"" message:@"请先绑定手环" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alterView show];
        return;
    }
    SRSilenceClockTableViewCellModel *silenceCellModel = (SRSilenceClockTableViewCellModel *)[_clocks objectAtIndex:indexPath.row];
    
    if (_srSilenceDelegate && [_srSilenceDelegate respondsToSelector:@selector(didSelectedClock:)])
    {
        [_srSilenceDelegate didSelectedClock:silenceCellModel];
    }

}


#pragma mark - SRSilenceClockTableViewCellDelegate

- (void)switchOn:(BOOL)on cell:(SRSilenceClockTableViewCell *)cell
{
    [cell setCustomWeekDay:cell.model openSwitch:on];
    
    cell.model.openSwitch = on;
        
    if (_srSilenceDelegate && [_srSilenceDelegate respondsToSelector:@selector(SRSilenceClockView:srSilenceClockTableViewCellModel:)])
    {
        [_srSilenceDelegate SRSilenceClockView:self srSilenceClockTableViewCellModel:cell.model];
    }
}


@end
